<template>
  <div class="works-view">
    <div class="container">
      <h1 class="page-title">我的作品</h1>
      
      <div class="works-header">
        <div class="stats">
          <div class="stat-item">
            <div class="stat-number">{{ totalWorks }}</div>
            <div class="stat-label">总作品数</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ totalViews }}</div>
            <div class="stat-label">总浏览量</div>
          </div>
          <div class="stat-item">
            <div class="stat-number">{{ totalLikes }}</div>
            <div class="stat-label">总点赞数</div>
          </div>
        </div>
        
        <div class="actions">
          <button @click="createNewWork" class="create-btn">
            <i class="fas fa-plus"></i>
            创建新作品
          </button>
          <div class="filter-group">
            <select v-model="selectedCategory" @change="filterWorks">
              <option value="">全部分类</option>
              <option value="video">视频</option>
              <option value="image">图片</option>
              <option value="text">文本</option>
              <option value="audio">音频</option>
            </select>
            <select v-model="sortBy" @change="sortWorks">
              <option value="date">按时间排序</option>
              <option value="views">按浏览量排序</option>
              <option value="likes">按点赞数排序</option>
            </select>
          </div>
        </div>
      </div>

      <div class="works-grid">
        <div 
          v-for="work in filteredWorks" 
          :key="work.id"
          class="work-card"
          @click="viewWork(work)"
        >
          <div class="work-preview">
            <img v-if="work.type === 'image'" :src="work.preview" :alt="work.title" />
            <div v-else class="work-placeholder">
              <i :class="getWorkIcon(work.type)"></i>
              <span>{{ work.type.toUpperCase() }}</span>
            </div>
            <div class="work-overlay">
              <div class="work-actions">
                <button @click.stop="editWork(work)" class="action-btn">
                  <i class="fas fa-edit"></i>
                </button>
                <button @click.stop="shareWork(work)" class="action-btn">
                  <i class="fas fa-share"></i>
                </button>
                <button @click.stop="deleteWork(work)" class="action-btn delete">
                  <i class="fas fa-trash"></i>
                </button>
              </div>
            </div>
            <div class="work-status" :class="work.status">
              {{ getStatusText(work.status) }}
            </div>
          </div>
          
          <div class="work-info">
            <h3 class="work-title">{{ work.title }}</h3>
            <p class="work-description">{{ work.description }}</p>
            <div class="work-meta">
              <div class="meta-item">
                <i class="fas fa-eye"></i>
                <span>{{ work.views }}</span>
              </div>
              <div class="meta-item">
                <i class="fas fa-heart"></i>
                <span>{{ work.likes }}</span>
              </div>
              <div class="meta-item">
                <i class="fas fa-calendar"></i>
                <span>{{ formatDate(work.createdAt) }}</span>
              </div>
            </div>
            <div class="work-tags">
              <span v-for="tag in work.tags" :key="tag" class="tag">
                {{ tag }}
              </span>
            </div>
          </div>
        </div>
      </div>

      <div v-if="filteredWorks.length === 0" class="empty-state">
        <div class="empty-icon">
          <i class="fas fa-folder-open"></i>
        </div>
        <h3>暂无作品</h3>
        <p>开始创建您的第一个作品吧！</p>
        <button @click="createNewWork" class="create-btn">
          <i class="fas fa-plus"></i>
          创建作品
        </button>
      </div>

      <div class="pagination" v-if="filteredWorks.length > 0">
        <button 
          @click="prevPage" 
          :disabled="currentPage === 1"
          class="page-btn"
        >
          <i class="fas fa-chevron-left"></i>
        </button>
        <span class="page-info">{{ currentPage }} / {{ totalPages }}</span>
        <button 
          @click="nextPage" 
          :disabled="currentPage === totalPages"
          class="page-btn"
        >
          <i class="fas fa-chevron-right"></i>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'

interface Work {
  id: string
  title: string
  description: string
  type: 'video' | 'image' | 'text' | 'audio'
  preview: string
  status: 'draft' | 'published' | 'archived'
  views: number
  likes: number
  tags: string[]
  createdAt: Date
}

const selectedCategory = ref('')
const sortBy = ref('date')
const currentPage = ref(1)
const itemsPerPage = 12

const works = ref<Work[]>([
  {
    id: '1',
    title: 'AI生成的艺术作品',
    description: '使用AI技术创作的独特艺术作品，展现了科技与艺术的完美结合。',
    type: 'image',
    preview: '/api/works/artwork-1.jpg',
    status: 'published',
    views: 1250,
    likes: 89,
    tags: ['AI艺术', '创意', '科技'],
    createdAt: new Date('2024-01-15')
  },
  {
    id: '2',
    title: '产品宣传视频',
    description: '为新产品制作的宣传视频，突出产品特色和优势。',
    type: 'video',
    preview: '/api/works/product-video.mp4',
    status: 'published',
    views: 3200,
    likes: 156,
    tags: ['产品', '宣传', '商业'],
    createdAt: new Date('2024-01-10')
  },
  {
    id: '3',
    title: '技术博客文章',
    description: '关于AI技术发展趋势的深度分析文章。',
    type: 'text',
    preview: '/api/works/blog-post.txt',
    status: 'draft',
    views: 0,
    likes: 0,
    tags: ['技术', 'AI', '博客'],
    createdAt: new Date('2024-01-20')
  },
  {
    id: '4',
    title: '背景音乐创作',
    description: 'AI辅助创作的背景音乐，适合各种场景使用。',
    type: 'audio',
    preview: '/api/works/background-music.mp3',
    status: 'published',
    views: 890,
    likes: 67,
    tags: ['音乐', '背景', '创作'],
    createdAt: new Date('2024-01-08')
  },
  {
    id: '5',
    title: '旅行vlog',
    description: '记录美好旅行时光的视频日志。',
    type: 'video',
    preview: '/api/works/travel-vlog.mp4',
    status: 'published',
    views: 2100,
    likes: 134,
    tags: ['旅行', 'vlog', '生活'],
    createdAt: new Date('2024-01-05')
  },
  {
    id: '6',
    title: '抽象艺术画作',
    description: 'AI生成的抽象艺术画作，色彩丰富，意境深远。',
    type: 'image',
    preview: '/api/works/abstract-art.jpg',
    status: 'archived',
    views: 450,
    likes: 23,
    tags: ['抽象', '艺术', '色彩'],
    createdAt: new Date('2024-01-12')
  }
])

const filteredWorks = computed(() => {
  let filtered = works.value

  // 按分类筛选
  if (selectedCategory.value) {
    filtered = filtered.filter(work => work.type === selectedCategory.value)
  }

  // 排序
  filtered = [...filtered].sort((a, b) => {
    switch (sortBy.value) {
      case 'views':
        return b.views - a.views
      case 'likes':
        return b.likes - a.likes
      case 'date':
      default:
        return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
    }
  })

  return filtered
})

const totalWorks = computed(() => works.value.length)
const totalViews = computed(() => works.value.reduce((sum, work) => sum + work.views, 0))
const totalLikes = computed(() => works.value.reduce((sum, work) => sum + work.likes, 0))
const totalPages = computed(() => Math.ceil(filteredWorks.value.length / itemsPerPage))

const getWorkIcon = (type: string) => {
  const icons = {
    video: 'fas fa-video',
    image: 'fas fa-image',
    text: 'fas fa-file-alt',
    audio: 'fas fa-music'
  }
  return icons[type as keyof typeof icons] || 'fas fa-file'
}

const getStatusText = (status: string) => {
  const statusTexts = {
    draft: '草稿',
    published: '已发布',
    archived: '已归档'
  }
  return statusTexts[status as keyof typeof statusTexts] || status
}

const formatDate = (date: Date) => {
  return new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: 'short',
    day: 'numeric'
  }).format(date)
}

const filterWorks = () => {
  currentPage.value = 1
}

const sortWorks = () => {
  currentPage.value = 1
}

const createNewWork = () => {
  alert('创建新作品')
}

const viewWork = (work: Work) => {
  alert(`查看作品: ${work.title}`)
}

const editWork = (work: Work) => {
  alert(`编辑作品: ${work.title}`)
}

const shareWork = (work: Work) => {
  alert(`分享作品: ${work.title}`)
}

const deleteWork = (work: Work) => {
  if (confirm(`确定要删除作品"${work.title}"吗？`)) {
    works.value = works.value.filter(w => w.id !== work.id)
    alert('作品已删除')
  }
}

const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--
  }
}

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++
  }
}
</script>

<style scoped>
.works-view {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 2rem 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

.page-title {
  text-align: center;
  color: white;
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 3rem;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.works-header {
  background: white;
  border-radius: 16px;
  padding: 2rem;
  margin-bottom: 3rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 2rem;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 2.5rem;
  font-weight: 700;
  color: #667eea;
  margin-bottom: 0.5rem;
}

.stat-label {
  color: #666;
  font-size: 1rem;
}

.actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.create-btn {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  padding: 1rem 2rem;
  border-radius: 50px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.create-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.4);
}

.filter-group {
  display: flex;
  gap: 1rem;
}

.filter-group select {
  padding: 0.75rem 1rem;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.3s ease;
}

.filter-group select:focus {
  border-color: #667eea;
}

.works-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
  margin-bottom: 3rem;
}

.work-card {
  background: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.work-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
}

.work-preview {
  height: 200px;
  position: relative;
  overflow: hidden;
}

.work-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.work-placeholder {
  width: 100%;
  height: 100%;
  background: #f8f9fa;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #6c757d;
}

.work-placeholder i {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.work-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.work-card:hover .work-overlay {
  opacity: 1;
}

.work-actions {
  display: flex;
  gap: 1rem;
}

.action-btn {
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  background: white;
  color: #667eea;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-btn:hover {
  transform: scale(1.1);
}

.action-btn.delete:hover {
  background: #dc3545;
  color: white;
}

.work-status {
  position: absolute;
  top: 1rem;
  right: 1rem;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 500;
}

.work-status.draft {
  background: #fff3cd;
  color: #856404;
}

.work-status.published {
  background: #d4edda;
  color: #155724;
}

.work-status.archived {
  background: #f8d7da;
  color: #721c24;
}

.work-info {
  padding: 1.5rem;
}

.work-title {
  color: #333;
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 0.5rem;
}

.work-description {
  color: #666;
  line-height: 1.5;
  margin-bottom: 1rem;
}

.work-meta {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  color: #6c757d;
  font-size: 0.9rem;
}

.work-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  background: #f0f4ff;
  color: #667eea;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.8rem;
}

.empty-state {
  text-align: center;
  background: white;
  border-radius: 16px;
  padding: 4rem 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.empty-icon {
  font-size: 4rem;
  color: #6c757d;
  margin-bottom: 2rem;
}

.empty-state h3 {
  color: #333;
  font-size: 1.5rem;
  margin-bottom: 1rem;
}

.empty-state p {
  color: #666;
  margin-bottom: 2rem;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  background: white;
  border-radius: 16px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.page-btn {
  width: 50px;
  height: 50px;
  border: 2px solid #e9ecef;
  border-radius: 50%;
  background: white;
  color: #667eea;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-btn:hover:not(:disabled) {
  border-color: #667eea;
  transform: scale(1.1);
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  color: #333;
  font-weight: 500;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }
  
  .stats {
    flex-direction: column;
    gap: 1rem;
  }
  
  .actions {
    flex-direction: column;
    align-items: stretch;
  }
  
  .filter-group {
    flex-direction: column;
  }
  
  .works-grid {
    grid-template-columns: 1fr;
  }
  
  .work-meta {
    flex-direction: column;
    gap: 0.5rem;
  }
}
</style> 